package base.exa18;


/**
 * StringBuffer线程安全
 */
public class StringBufferStudy{

    public static void main(String[] args) {
        StringBufferStudy stringBufferStudy = new StringBufferStudy();
        stringBufferStudy.study01();
        //stringBufferStudy.study02();
        //stringBufferStudy.study03();
         //stringBufferStudy.appendStudy();
         //stringBufferStudy.charAtStudy();
         //stringBufferStudy.insertStudy();
         //stringBufferStudy.reverseStudy();
         //stringBufferStudy.deleteStudy();
         stringBufferStudy.replaceStudy();
         stringBufferStudy.lengthStudy();
    }


    /**
     * 默认构造方法学习
     */
    public void study01(){
        //新建一个新的StringBuffer对象
        StringBuffer stringBuffer = new StringBuffer();
        System.out.println("使用new StringBuffer()构造的对象其容量大小为："+stringBuffer.capacity());
        stringBuffer.append("1111123434433233");
        System.out.println("扩容机制为：2*oldCapacity+2");
        stringBuffer.append("1");
        //扩容: int newCapacity = (value.length << 1) + 2;  ->  2*oldCapacity+2
        System.out.println("第一次扩容：16*2+2= "+stringBuffer.capacity());
        stringBuffer.append("11111111111111112222221");
        System.out.println("第二次扩容：34*2+2= "+stringBuffer.capacity());
    }

    /**
     *构造时指定容量大小
     */
    public void study02(){
        StringBuffer stringBuffer = new StringBuffer(5);
        System.out.println("使用new StringBuffer(5)构造的对象其容量大小为："+stringBuffer.capacity());
        stringBuffer.append("11111");
        System.out.println("扩容机制为：2*oldCapacity+2");
        stringBuffer.append("1");
        //扩容: int newCapacity = (value.length << 1) + 2;  ->  2*oldCapacity+2
        System.out.println("第一次扩容：5*2+2= "+stringBuffer.capacity());
        stringBuffer.append("1111111");
        System.out.println("第二次扩容：12*2+2= "+stringBuffer.capacity());
    }

    /**
     * 字符串构造
     */
    public void study03(){
        StringBuffer stringBuffer = new StringBuffer("111");
        System.out.println("使用new StringBuffer(\"111\")构造的对象其容量大小为："
                +stringBuffer.capacity());
    }

    /**
     * StringBuffer append(String s); 学习
     * 我们知道String的字符串拼接是直接用 + 就可以的，
     * 但是使用StringBuffer，我们就得使用特定的方法去进行字符串的拼接
     * append方法,此方法也能进行不同类型的数据拼接进字符串
     */
    public void appendStudy(){
        StringBuffer stringBuffer = new StringBuffer("111");
        stringBuffer.append("nihao");
        System.out.println(stringBuffer.toString());
        stringBuffer.append(1222);
        System.out.println(stringBuffer.toString());
        stringBuffer.append('a');
        System.out.println(stringBuffer.toString());//.....等等
        stringBuffer.append(new Object());
        System.out.println(stringBuffer.toString());
    }

    /**
     *charAt()方法获取指定索引位置的字符，setCharAt方法重置指定索引位置的字符
     */
    public void charAtStudy(){
        StringBuffer stringBuffer = new StringBuffer("123");
        System.out.println(stringBuffer.charAt(1));
        stringBuffer.setCharAt(1,'4');
        System.out.println(stringBuffer.toString());
    }

    /**
     *insert在指定位置插入指定字符
     */
    public void insertStudy(){
        StringBuffer stringBuffer = new StringBuffer("123");
        stringBuffer.insert(1,6);
        System.out.println(stringBuffer.toString());
    }

    /**
     * reverse将字符序列进行反转
     */
    public void reverseStudy(){
        StringBuffer stringBuffer = new StringBuffer("123");
        stringBuffer.reverse();
        System.out.println(stringBuffer.toString());
    }

    /**
     *  delete(int startIndex,int endIndex)
     *  delete(int index)学习
     */
    public void deleteStudy(){
        StringBuffer stringBuffer = new StringBuffer("1235678");
        System.out.println(stringBuffer.delete(0,3).toString());
        System.out.println(stringBuffer.deleteCharAt(1).toString());
    }


    /**
     *  replace(int startIndex,int endIndex,String str)
     */
    public void replaceStudy(){
        StringBuffer stringBuffer = new StringBuffer("1235678");
        stringBuffer.replace(0,3,"hhhh");
        System.out.println(stringBuffer.toString());
    }

    /**
     * length方法获取实体中存放的字符序列的长度，
     * 也可调用capacity方法获取当前实体的实际容量。
     */
    public void lengthStudy(){
        StringBuffer stringBuffer = new StringBuffer("1235678");
        System.out.println("stringBuffer length: "+stringBuffer.length());
        System.out.println("stringBuffer capacity: "+stringBuffer.capacity());
    }




}
